<!--
 * @Author: weikang
 * @Description:
 * @Date: 2023-12-24 20:17:37
 * @LastEditTime: 2024-05-08 21:29:36
 * @FilePath: \env-manager-pc\src\views\document\manager\create\main_table\main.table.vue
-->
<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'
import { useEntPageData } from '../createHelper'
import mainTableEdit from './main.table.edit.vue'
import type { MainTableRow } from '@/views/document/manager/create/create'
import useEntStore from '@/store/modules/ent'
import type { doLoading } from '@/types/page'
import entApi from '@/api/modules/ent'
import { ModelType } from '@/types/common'

const entStore = useEntStore()
const { currentEntData, refreshDevices } = entStore
const { currentEntLoading } = storeToRefs(entStore)
const tableData = computed(() => {
  return currentEntData.ElectricDevices
})
const editRef = ref()

const { isReadOnly } = useEntPageData()

function toCreate() {
  editRef.value.open({ type: ModelType.add })
}

function toEdit(row: any) {
  editRef.value.open({ type: ModelType.edit, data: row })
}

function toCheck(row: any) {
  editRef.value.open({ type: ModelType.readonly, data: row })
}

async function submitEdit(formData: MainTableRow, doLoading: doLoading) {
  try {
    doLoading(true)
    const baseInfoObjID = currentEntData?.baseInfo?.ObjID
    if (!baseInfoObjID) {
      return
    }
    const isCreate = !formData.ObjID
    isCreate
      ? await entApi.AddElectricDevice({
        enterpriseObjID: baseInfoObjID,
        electricDevice: formData,
      })
      : await entApi.UpdateElectricDevice({
        electricDevice: formData,
        electricDeviceObjID: formData.ObjID,
      })
    refreshDevices(baseInfoObjID)
    editRef.value.close()
    ElMessage.success({
      message: isCreate ? '新增成功' : '编辑成功',
      type: 'success',
    })
  }
  catch (e) {
    doLoading(false)
  }
}

async function delEnt(row: MainTableRow) {
  await ElMessageBox.confirm(`确定删除总表"${row.Name}" 吗？`, '提示', {
    type: 'warning',
  })
  const baseInfoObjID = currentEntData?.baseInfo?.ObjID as string
  await entApi.DeleteElectricDevice({ electricDeviceObjID: row.ObjID, enterpriseObjID: baseInfoObjID })
  ElMessage.success({
    message: '删除成功',
    type: 'success',
  })
  refreshDevices(currentEntData.baseInfo?.ObjID)
}
</script>

<template>
  <div>
    <div v-if="!isReadOnly" class="mb-4 flex flex-justify-end">
      <el-button type="success" @click="toCreate">
        新增总表
      </el-button>
    </div>
    <div v-auto-height="50">
      <vxe-table
        auto-resize border
        height="100%"
        :loading="currentEntLoading"
        :data="tableData"
        :row-config="{ isCurrent: true }"
      >
        <VxeColumn field="Name" title="总表名称" min-width="100" />
        <vxe-column field="AssetCode" title="资产号" min-width="100" />
        <VxeColumn field="MN" title="MN号" min-width="100" />
        <VxeColumn field="PointCode" title="采集因子" min-width="100" />
        <VxeColumn field="Location" title="安装位置" width="150" />
        <VxeColumn field="operate" title="操作" width="160">
          <template #default="{ row }">
            <ElButton link type="primary" @click="toCheck(row)">
              查看
            </ElButton>
            <ElButton v-if="!isReadOnly" link type="success" @click="toEdit(row)">
              编辑
            </ElButton>
            <ElButton v-if="!isReadOnly" link type="danger" @click="delEnt(row)">
              删除
            </ElButton>
          </template>
        </VxeColumn>
      </vxe-table>
    </div>
    <mainTableEdit ref="editRef" @submit="submitEdit" />
  </div>
</template>
